jQuery(document).ready(function ($) {
    $(".design-img-container").click(function (e) {
        e.preventDefault();
        if ($(this).children().length > 0) {
            var img = $(this).children("img").attr("src")

        } else {
            var desinurl = $(this).data("design");
            if (!desinurl) {
                alert("no desin url");
                return;
            }
            location.href = desinurl;
            // location.href = 'http://localhost:8000/?page_id=13';
        }
    });


    var designImgCookie = getCookie('design_img');
    var designIdCookie = getCookie('design_id');
    var designColorCookie = getCookie('design_color');
    console.log('回调后参数：',designImgCookie, designIdCookie, designColorCookie);
    if (designImgCookie && designIdCookie && designColorCookie) {
        $(".design-img-container").html(`<div class="clear_design">x</div><a href="${designImgCookie}" data-lightbox="image-1" data-title="Design"><img src="${designImgCookie}"/></a>`);
        $(".design-product-detail").append(`<div class="design-row"><div class="design-title">Design color:</div><div class="design-value"><div class="design_color" style="background-color:#${designColorCookie}">${designColorCookie}</div></div>`);
        // $(".design-product-detail").append(`<div class="design-row"><div class="design-title">Design Id:</div><div class="design-value">${designIdCookie}</div>`);
        $("#design_img").val(designImgCookie);
        $("#design_id").val(designIdCookie);
        $("#design_color").val(designColorCookie);
        $(".design-img-container").width('100px')
        $(".design-img-container").removeClass('design-img-box')
    }



    $(".clear_design").click(function (e) {
        e.preventDefault();
        e.stopPropagation();
        $(".design-img-container").empty();
        $(".design-row:not(:first)").remove();
        $("#design_img").val("");
        $("#design_id").val("");
        $("#design_color").val("");
        deleteCookie("design_img");
        deleteCookie("design_id");
        deleteCookie("design_color");
        $(".design-img-container").width('200px')
        $(".design-img-container").addClass('design-img-box')
        $("#design-row").remove();
    });


    // setTimeout(function() {
    //     $(".wc-block-components-product-details__design").each(function (index, element) {
    //         var desingdom=$(element).find(".wc-block-components-product-details__value");
    //         var designImg = desingdom.text();
    //         desingdom.html(`<a href="${designImg}" data-lightbox="image-1" data-title="Design"><img src="${designImg}"/></a>`);
    //         console.log(designImg);
    //     });
    // }, 1000);

});

function getCookie(cookieName) {
    var name = cookieName + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var cookieArray = decodedCookie.split(';');
    for (var i = 0; i < cookieArray.length; i++) {
        var cookie = cookieArray[i];
        while (cookie.charAt(0) == ' ') {
            cookie = cookie.substring(1);
        }
        if (cookie.indexOf(name) == 0) {
            return cookie.substring(name.length, cookie.length);
        }
    }
    return "";
}

function deleteCookie(cookieName) {
    document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}

function cleanPage() {
    $('.wp-block-template-part').remove();
    $('.header-media-inner-post').remove();
    $('.wp-site-blocks').removeClass();

    // 获取所有包含特定类的元素
    var elements = document.querySelectorAll('.wp-block-template-part.header-media-inner-post');
    // 遍历每个元素并移除类
    elements.forEach(function(element) {
        element.remove();
    });

    var elements = document.querySelectorAll('.wp-site-blocks');
    // 遍历每个元素并移除类
    elements.forEach(function(element) {
        element.classList.remove('wp-site-blocks')
    });



}


